import { Indicator } from "mint-ui";
import axios from "axios";
import store from "@/store/index.js"
import router from "@/router/index.js"



export default {
  common: {
    method: "GET",
    data: {},
    params: {},
	headers:{}
  },
  $axios(options = {}) {
    options.method = options.method || this.common.method;
    options.data = options.data || this.common.data;
    options.params = options.params || this.common.params;
	options.headers = options.headers || this.common.headers;

    //请求前==》显示加载中...
    Indicator.open({
      text: "Loading...",
      spinnerType: "fading-circle",
    });
	
	//是否是登录状态
	if(options.headers.token){
		options.headers.token = store.state.user.token;
		// console.log(options.headers.token)
		if( !options.headers.token){
			//没有token跳转到登录页
			router.push('/login');
		}
	}

    return axios(options).then((v) => {
      let data = v.data.data;
	  // console.log(data)
	  //如果token过期出现登录
	  if(data.code == 1000){
		  //本地用户信息删除
		  localStorage.removeItem("teaUseraInfor")
		  console.log('code==1000')
		  Indicator.close();
		  return router.replace('/userLogin')
	  }else{
		  return new Promise((res, rej) => {
		    //请求不到数据返回
		    if (!v) return rej();
		    //关闭==》加载中
		    setTimeout(() => {
		      Indicator.close();
		    }, 500);
		    //返回数据
		    res(data);
		  });
	  }
	  
      
    });
  },
};
